<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            margin: 0 auto;
            border: 1px solid black;
            background-color: bisque;
            position: absolute;
            transition: all linear;
        }
    </style>
</head>
<div id="box">

</div>

<body>
    <script>
        var arr = []

        box.onmousedown = function (event) {
            arr = []
            event.preventDefault();
            var reX = event.offsetX
            var reY = event.offsetY
            document.onmousemove = function (event) {

                box.style.left = event.clientX - reX + 'px'
                box.style.top = event.clientY - reY + 'px'
                arr.push({ left: box.style.left, top: box.style.top })

            }
        }
        document.onmouseup = function () {
            document.onmousemove = null
            if (arr.length > 0) {
                backPlay()
            }


        }

        function backPlay() {
            var index = arr.length - 1
            var myTimer = setInterval(() => {
                if (index < 0) {
                    clearInterval(myTimer)
                    box.style.left = 0
                    box.style.top = 0
                    arr = []
                    return
                }
                box.style.left = arr[index--].left
                box.style.top = arr[index--].top

            }, 30)
        }
    </script>
</body>

</html>